<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入商品名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag type="list" v-model="model.categoryId"
                dictCode="gallery_goods_category,category_name,id,is_delete='0'" placeholder="请选择商品分类" />
              <!--              <a-select show-search placeholder="请选择社区农村小区" :options="selectList" style="width:100% " @focus="handleFocus"-->
              <!--                        @blur="handleBlur" v-model="model.linkId" @change="handleChange" :filter-option="filterOption">-->
              <!--              </a-select>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input v-model="model.price" placeholder="请输入价格"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="spec">
              <a-input v-model="model.spec" placeholder="请输入规格"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="material">
              <a-input v-model="model.material" placeholder="请输入材质"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品详情简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="carouselMap">
              <j-image-upload isMultiple v-model="model.carouselMap" bizPath=""></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上传视频" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="videoUrl">
              <j-video-upload v-model="model.videoUrl" :value="model.videoUrl" :videoFirst="model.videoFirst"
                @fileList1="fileList" file-type="video"></j-video-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否上下架" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <!-- <a-input-number v-model="model.status" placeholder="请输入1上架 2下架" style="width: 100%" /> -->
              <a-switch size="middle" v-model="model.status === 1" @change="handleSwitchChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stock">
              <a-input-number v-model="model.stock" placeholder="请输入库存数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="浏览量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="browseNum">
              <a-input-number v-model="model.browseNum" placeholder="请输入浏览量" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="浏览量123" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="browseNum">
              <a-input-number v-model="model.browseNum1" placeholder="请输入浏览量" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="0正常 1删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDelete">
              <a-input-number v-model="model.isDelete" placeholder="请输入0正常 1删除" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="分类id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <a-input-number v-model="model.categoryId" placeholder="请输入分类id" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'


export default {
  name: 'GalleryGoodsForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        // videoUrl:'',
        // videoFirst:'',
        status: 2, // 默认状态值
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入商品名称!' },
        ],
        content: [
          { required: true, message: '请输入商品详情简介!' },
        ],
        carouselMap: [
          { required: true, message: '请输入轮播图!' },
        ],
        status: [
          { required: true, message: '请输入1上架 2下架!' },
        ],
        stock: [
          { required: true, message: '请输入库存数量!' },
        ],
        browseNum: [
          { required: true, message: '请输入浏览量!' },
        ],
        isDelete: [
          { required: true, message: '请输入0正常 1删除!' },
        ],
        categoryId: [
          { required: true, message: '请输入分类id!' },
        ],

      },
      url: {
        add: "/gallery/galleryGoods/add",
        edit: "/gallery/galleryGoods/edit",
        queryById: "/gallery/galleryGoods/queryById",
        // file:alumniFile
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  watch: {
    videoPreviewVisible: function (val) {
      if (val === false) {
        const video = document.getElementById('video')
        video.pause()
      }
    }
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      console.log("asdfasdfasdfasdfadsfasd", record)
      this.model = Object.assign({}, record);
      console.log(this.model, 'model');
      this.visilpble = true;
    },
    handleSwitchChange(checked) {
      this.model.status = checked ? 1 : 2;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    fileList(e) {
      console.log(e[0], 'e');
      this.model.videoUrl = e[0].url
      this.model.videoFirst = e[0].thumbUrl
      console.log(this.model, 'model');
    }
  }
}
</script>